<template>
  <div style="">
    <!-- 云际服务提供者 -->
    <div style="margin-left: 48px; margin-bottom: 20px; margin-top: 16px;">
      <div style="margin-bottom: 20px;">
        <el-radio-group v-model="radio1" @change="changeRadio">
          <el-radio-button label="2">云际资源</el-radio-button>
          <el-radio-button label="1">业务资源</el-radio-button>
        </el-radio-group>
      </div>
      <div v-show="radio1 == '1'">
        <el-checkbox-group v-model="checkList" @change="changeCheckList">
          <el-checkbox label="A">审批办公</el-checkbox>
          <el-checkbox label="B">信息共享</el-checkbox>
          <el-checkbox label="C">防汛防灾</el-checkbox>
          <el-checkbox label="D">电子采购</el-checkbox>
          <el-checkbox label="E">电子税务</el-checkbox>
          <el-checkbox label="F">证照办理</el-checkbox>
          <el-checkbox label="G">咨询服务</el-checkbox>
          <el-checkbox label="H">企业服务</el-checkbox>
          <el-checkbox label="I">公文处理</el-checkbox>
          <el-checkbox label="J">数据平台</el-checkbox>
        </el-checkbox-group>
        <div style="margin-top: 15px;">
          <label>
            根据所选业务推荐云商： {{ baseForm.consultId ? consultList.filter(c => c.id == baseForm.consultId)[0].name : '' }}
          </label>
        </div>
      </div>
      <div v-show="radio1 == '2'">
        <label style="font-weight: bold; font-size: 10pt;">云际服务提供者：</label>
        <el-select v-model="baseForm.consultId" placeholder="请选择云提供者" @change="changeConsult">
          <el-option v-for="item in consultList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <span style="margin-left: 20px; color: #a2a2a2; font-size: 12px;">
          {{ baseForm.consultId ? consultList.filter(c => c.id == baseForm.consultId)[0].remark : '' }}
        </span>
      </div>
    </div>
    <!-- 私有云 -->
    <div style="margin-top: 10px; margin-left: 30px;" v-if="baseForm.cloudType == 'PRIVATE' && baseForm.consultId != null && baseForm.consultId != ''">
      <private-create :vId="baseForm.vId"></private-create>
    </div>
    <div style="margin-top: 10px;margin-left: 50px;margin-bottom: 20px;" v-if="baseForm.cloudType == 'PRIVATE' && (baseForm.consultId == null || baseForm.consultId == '')">
      <span>没有适合的私有云</span>
    </div>
    <!-- 公有云 -->
    <div style="margin-top: 10px;" v-if="baseForm.cloudType == 'PUBLIC' && baseForm.consultId != null && baseForm.consultId != ''">
      <pub-create :vId="baseForm.vId"></pub-create>
    </div>
    <div style="margin-top: 10px;margin-left: 50px;margin-bottom: 20px;" v-if="baseForm.cloudType == 'PUBLIC' && (baseForm.consultId == null || baseForm.consultId == '')">
      <span>没有适合的公有云</span>
    </div>
  </div>
</template>

<script>
import PubCreate from '../vmList/index.vue'
import PraviteCreate from '../../admin/vmHost/createVM'
import request from '@/utils/request'

export default {
  name: 'newCreateVM',
  components: {
    'pub-create': PubCreate, // 注册子页组件
    'private-create': PraviteCreate
  },
  data() {
    return {
      radio1: '2',
      checkList: [],
      consultList: [],
      baseForm: {
        consultId: '',
        cloudType: '',
        vId: ''
      }
    }
  },
  async mounted() {
    await this.getConsultList()
  },
  methods: {
    // 切换标签和手动选择
    changeRadio() {
      this.checkList = []
      this.baseForm.consultId = ''
      this.baseForm.cloudType = ''
      this.baseForm.vId = ''
      this.getConsultList()
    },
    // 磋商列表
    getConsultList() {
      let _self = this
      request({
        url: '/cloud/jcce/v1.0/consult/list?pageNumber=1&pageSize=1000',
        method: 'get',
      }).then(({data}) => {
        console.log("磋商列表:")
        _self.consultList = data.pageData
        if (_self.consultList.length > 0) {
          _self.baseForm.consultId = _self.consultList[0].id
          _self.changeConsult()
        }
      })
    },
    changeConsult() {
      let x = this.consultList.filter(c => c.id == this.baseForm.consultId)[0]
      // 公有云
      if (x.cloudPlatformIds != '' && x.cloudPlatformIds != null) {
        this.baseForm.cloudType = 'PUBLIC'
        try {
          // 特殊逻辑 如果公有云包含腾讯，那么优先选择腾讯
          let stringArray = x.cloudPlatformNames.split('_')
          const index = stringArray.findIndex(str => str.includes("火山"));
          if (index !== -1) {
            // 找到腾讯
            console.log("找到腾讯")
            this.baseForm.vId = x.cloudPlatformIds.split(',')[index]
          } else {
            // 没找到腾讯，默认选择第一个
            console.log("没找到腾讯")
            this.baseForm.vId = x.cloudPlatformIds.split(',')[0]
          }
        } catch (e) {
          console.error(e)
          // 存在异常，默认选择第一个
          this.baseForm.vId = x.cloudPlatformIds.split(',')[0]
        }
      }
      // 私有云
      else if (x.privatePlatformIds != '' && x.privatePlatformIds != null) {
        this.baseForm.cloudType = 'PRIVATE'
        this.baseForm.vId = x.privatePlatformIds.split(',')[0]
      }
    },
    changeCheckList() {
      let _self = this
      // 选公有云逻辑
      if (_self.checkList.indexOf("B") > -1 || _self.checkList.indexOf("C") > -1 || _self.checkList.indexOf("F") > -1
        || _self.checkList.indexOf("G") > -1|| _self.checkList.indexOf("H") > -1) {
        _self.baseForm.cloudType = 'PUBLIC'
        let pubLength = _self.consultList.filter(c => c.cloudPlatformIds != null && c.cloudPlatformIds != '').length
        if (pubLength == 0) {
          console.log('not found public')
          _self.baseForm.consultId = ''
        } else {
          console.log('select public')
          _self.selectPublic();
        }
      }
      // 选私有云逻辑
      else {
        _self.baseForm.cloudType = 'PRIVATE'
        let privateLength = _self.consultList.filter(c => c.privatePlatformIds != null && c.privatePlatformIds != '').length
        if (privateLength == 0) {
          console.log('not found private')
          _self.baseForm.consultId = ''
        } else {
          console.log('select private')
          _self.selectPrivate();
        }
      }
    },
    selectPublic() {
      let list = this.consultList.filter(c => c.cloudPlatformIds != null && c.cloudPlatformIds != '')
      // 使每次选择对应固定的磋商结果
      let selectListStr = this.checkList.sort().toString();
      console.log('selectListStr:' + selectListStr);
      let selectIdx = this.stringToHash(selectListStr) % list.length;
      this.baseForm.consultId = list[selectIdx].id
      this.changeConsult()
    },
    selectPrivate() {
      let list = this.consultList.filter(c => c.privatePlatformIds != null && c.privatePlatformIds != '')
      // 使每次选择对应固定的磋商结果
      let selectListStr = this.checkList.sort().toString();
      console.log('selectListStr:' + selectListStr);
      let selectIdx = this.stringToHash(selectListStr) % list.length;
      this.baseForm.consultId = list[selectIdx].id
      this.changeConsult()
    },
    // 计算字符串数字哈希
    stringToHash(str) {
      let hash = 0;
      for (let i = 0; i < str.length; i++) {
        const char = str.charCodeAt(i);
        hash = (hash + char) * 233 + 131337; // 使用一个随机的大质数来增加散列度
      }
      return hash;
    }

  }
}
</script>
